home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / bbs / termv4.6 / extras / source / term-source.lha / Scale.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-18  |  7.1 KB  |  382 lines

  1. /*
  2. **    Scale.c
  3. **
  4. **    Single scaled character output routines
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #ifndef _GLOBAL_H
  11. #include "Global.h"
  12. #endif
  13.  
  14.     /* Some static data required by the bitmap scaling routine. */
  15.  
  16. STATIC struct RastPort        *ScaleRPort;
  17. STATIC struct BitMap        *ScaleSrcBitMap,
  18.                 *ScaleDstBitMap;
  19. STATIC struct BitScaleArgs    *ScaleArgs;
  20.  
  21. STATIC WORD             ScaleCache    = -1,
  22.                  ScaleType    = 42,
  23.                  ScaleConfig    = 42;
  24.  
  25.     /* DeleteScale():
  26.      *
  27.      *    Frees all the data associated with font scaling.
  28.      */
  29.  
  30. VOID
  31. DeleteScale()
  32. {
  33.     FreeVecPooled(ScaleArgs);
  34.     ScaleArgs = NULL;
  35.  
  36.     DeleteBitMap(ScaleDstBitMap);
  37.     ScaleDstBitMap = NULL;
  38.  
  39.     DeleteBitMap(ScaleSrcBitMap);
  40.     ScaleSrcBitMap = NULL;
  41.  
  42.     FreeVecPooled(ScaleRPort);
  43.     ScaleRPort = NULL;
  44. }
  45.  
  46.     /* CreateScale():
  47.      *
  48.      *    Sets up the data required for real-time font scaling
  49.      *    (bitmaps, rastports, etc.).
  50.      */
  51.  
  52. BOOL
  53. CreateScale(struct Window *Parent)
  54. {
  55.         /* Create a RastPort to render into. */
  56.  
  57.     if(ScaleRPort = (struct RastPort *)AllocVecPooled(sizeof(struct RastPort),MEMF_ANY | MEMF_CLEAR))
  58.     {
  59.         LONG    MaxWidth,
  60.             PlaneWidth,
  61.             PlaneHeight;
  62.         LONG    Depth;
  63.  
  64.             /* Initialize it. */
  65.  
  66.         InitRastPort(ScaleRPort);
  67.  
  68.         if(GFX)
  69.             MaxWidth = GFX -> tf_XSize;
  70.         else
  71.             MaxWidth = 0;
  72.  
  73.         if(TextFontWidth > MaxWidth)
  74.             MaxWidth = TextFontWidth;
  75.  
  76.             /* Remember dimensions. */
  77.  
  78.         PlaneWidth    = MaxWidth;
  79.         PlaneHeight    = TextFontHeight;
  80.  
  81.             /* Check the depth of the original screen. */
  82.  
  83.         Depth = GetBitMapDepth(Parent -> RPort -> BitMap);
  84.  
  85.             /* Create the bitmap to render into. */
  86.  
  87.         if(ScaleSrcBitMap = (struct BitMap *)CreateBitMap(PlaneWidth,PlaneHeight,Depth,BMF_CLEAR,Parent -> RPort -> BitMap))
  88.         {
  89.                 /* Create the bitmap to place the scaled font data into. */
  90.  
  91.             if(ScaleDstBitMap = (struct BitMap *)CreateBitMap(PlaneWidth * 2,PlaneHeight * 2,Depth,BMF_CLEAR,Parent -> RPort -> BitMap))
  92.             {
  93.                     /* Put the source bitmap into the source RastPort. */
  94.  
  95.                 ScaleRPort -> BitMap = ScaleSrcBitMap;
  96.  
  97.                     /* Install the fonts. */
  98.  
  99.                 SetFont(ScaleRPort,CurrentFont);
  100.  
  101.                     /* Set the default rendering pens. */
  102.  
  103.                 SetPens(ScaleRPort,1,0,JAM2);
  104.  
  105.                     /* Allocate space for the bitmap scaling arguments. */
  106.  
  107.                 if(ScaleArgs = (struct BitScaleArgs *)AllocVecPooled(sizeof(struct BitScaleArgs),MEMF_ANY | MEMF_CLEAR))
  108.                 {
  109.                         /* Initialize the structure. */
  110.  
  111.                     ScaleArgs -> bsa_SrcWidth    = TextFontWidth;
  112.                     ScaleArgs -> bsa_SrcHeight    = TextFontHeight;
  113.  
  114.                     ScaleArgs -> bsa_YSrcFactor    = 1;
  115.  
  116.                     ScaleArgs -> bsa_SrcBitMap    = ScaleSrcBitMap;
  117.                     ScaleArgs -> bsa_DestBitMap    = ScaleDstBitMap;
  118.  
  119.                     return(TRUE);
  120.                 }
  121.             }
  122.         }
  123.     }
  124.  
  125.     return(FALSE);
  126. }
  127.  
  128.     /* PrintScaled(UBYTE Char,UBYTE Scale):
  129.      *
  130.      *    This is the big one: since VT100 supports a number of
  131.      *    font sizes (double height, double width, 132 columns),
  132.      *    the appropriate characters are scaled in real-time before
  133.      *    they are displayed.
  134.      */
  135.  
  136. VOID
  137. PrintScaled(STRPTR Buffer,LONG Size,LONG Scale)
  138. {
  139.     LONG SrcY,DestX,DestY,SizeX;
  140.  
  141.         /* Determine the scale of the destination character. */
  142.  
  143.     if(CurrentCharWidth == SCALE_HALF)
  144.     {
  145.             /* Determine scale to be used. */
  146.  
  147.         switch(Scale)
  148.         {
  149.                 /* Half width. */
  150.  
  151.             case SCALE_ATTR_NORMAL:
  152.  
  153.                 ScaleArgs -> bsa_XDestFactor    = 1;
  154.                 ScaleArgs -> bsa_YDestFactor    = 1;
  155.                 ScaleArgs -> bsa_XSrcFactor    = 2;
  156.  
  157.                 SrcY    = 0;
  158.                 DestX    = MUL_X(CursorX) / 2;
  159.                 SizeX    = TextFontWidth / 2;
  160.  
  161.                 ScaleCache = -1;
  162.  
  163.                 break;
  164.  
  165.                 /* Half width, double height (top bits). */
  166.  
  167.             case SCALE_ATTR_TOP2X:
  168.  
  169.                 ScaleArgs -> bsa_XDestFactor    = 1;
  170.                 ScaleArgs -> bsa_YDestFactor    = 2;
  171.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  172.  
  173.                 SrcY    = 0;
  174.                 DestX    = MUL_X(CursorX);
  175.                 SizeX    = TextFontWidth;
  176.  
  177.                 ScaleCache = -1;
  178.  
  179.                 break;
  180.  
  181.                 /* Half width, double height (bottom bits). */
  182.  
  183.             case SCALE_ATTR_BOT2X:
  184.  
  185.                 ScaleArgs -> bsa_XDestFactor    = 1;
  186.                 ScaleArgs -> bsa_YDestFactor    = 2;
  187.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  188.  
  189.                 SrcY    = TextFontHeight;
  190.                 DestX    = MUL_X(CursorX);
  191.                 SizeX    = TextFontWidth;
  192.  
  193.                 ScaleCache = -1;
  194.  
  195.                 break;
  196.         }
  197.     }
  198.     else
  199.     {
  200.             /* Determine scale to be used. */
  201.  
  202.         switch(Scale)
  203.         {
  204.                 /* Double height (top bits). */
  205.  
  206.             case SCALE_ATTR_TOP2X:
  207.  
  208.                 ScaleArgs -> bsa_XDestFactor    = 2;
  209.                 ScaleArgs -> bsa_YDestFactor    = 2;
  210.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  211.  
  212.                 SrcY    = 0;
  213.                 DestX    = MUL_X(CursorX) * 2;
  214.                 SizeX    = TextFontWidth * 2;
  215.  
  216.                 ScaleCache = -1;
  217.  
  218.                 break;
  219.  
  220.                 /* Double height (bottom bits). */
  221.  
  222.             case SCALE_ATTR_BOT2X:
  223.  
  224.                 ScaleArgs -> bsa_XDestFactor    = 2;
  225.                 ScaleArgs -> bsa_YDestFactor    = 2;
  226.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  227.  
  228.                 SrcY    = TextFontHeight;
  229.                 DestX    = MUL_X(CursorX) * 2;
  230.                 SizeX    = TextFontWidth * 2;
  231.  
  232.                 ScaleCache = -1;
  233.  
  234.                 break;
  235.  
  236.                 /* Double width. */
  237.  
  238.             case SCALE_ATTR_2X:
  239.  
  240.                 ScaleArgs -> bsa_XDestFactor    = 2;
  241.                 ScaleArgs -> bsa_YDestFactor    = 1;
  242.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  243.  
  244.                 SrcY    = 0;
  245.                 DestX    = MUL_X(CursorX) * 2;
  246.                 SizeX    = TextFontWidth * 2;
  247.  
  248.                 ScaleCache = -1;
  249.  
  250.                 break;
  251.         }
  252.     }
  253.  
  254.     ScaleType    = Scale;
  255.     ScaleConfig    = CurrentCharWidth;
  256.  
  257.         /* Look for the font type to scale. */
  258.  
  259.     if(ScaleRPort -> Font != CurrentFont)
  260.     {
  261.         SetFont(ScaleRPort,CurrentFont);
  262.  
  263.         ScaleArgs -> bsa_SrcWidth = TextFontWidth;
  264.  
  265.         ScaleCache = -1;
  266.     }
  267.  
  268.         /* Set the appropriate colours. */
  269.  
  270.     if(ReadAPen(ScaleRPort) != ReadAPen(RPort))
  271.     {
  272.         SetAPen(ScaleRPort,ReadAPen(RPort));
  273.  
  274.         ScaleCache = -1;
  275.     }
  276.  
  277.     if(ReadBPen(ScaleRPort) != ReadBPen(RPort))
  278.     {
  279.         SetBPen(ScaleRPort,ReadBPen(RPort));
  280.  
  281.         ScaleCache = -1;
  282.     }
  283.  
  284.         /* Calculate topmost line to write to. */
  285.  
  286.     DestY = MUL_Y(CursorY);
  287.  
  288.     if(CurrentFont == GFX)
  289.     {
  290.         LONG Mode = 1;
  291.  
  292.             /* Run down the buffer... */
  293.  
  294.         while(Size--)
  295.         {
  296.             if(GfxTable[*Buffer] == Mode)
  297.             {
  298.                 if(*Buffer != ScaleCache)
  299.                 {
  300.                     ScaleCache = *Buffer;
  301.  
  302.                         /* Print the character to be scaled into the
  303.                          * invisible drawing area.
  304.                          */
  305.  
  306.                     PlaceText(ScaleRPort,0,0,Buffer++,1);
  307.  
  308.                         /* Scale the font. */
  309.  
  310.                     BitMapScale(ScaleArgs);
  311.                 }
  312.                 else
  313.                     Buffer++;
  314.  
  315.                     /* Render the character. */
  316.  
  317.                 BltBitMapRastPort(ScaleDstBitMap,0,SrcY,RPort,WindowLeft + DestX,WindowTop + DestY,SizeX,TextFontHeight,MINTERM_COPY);
  318.             }
  319.             else
  320.             {
  321.                 ScaleCache = *Buffer;
  322.  
  323.                 if(Mode)
  324.                     SetFont(ScaleRPort,TextFont);
  325.                 else
  326.                     SetFont(ScaleRPort,GFX);
  327.  
  328.                 Mode ^= 1;
  329.  
  330.                     /* Print the character to be scaled into the
  331.                      * invisible drawing area.
  332.                      */
  333.  
  334.                 PlaceText(ScaleRPort,0,0,Buffer++,1);
  335.  
  336.                     /* Scale the font. */
  337.  
  338.                 BitMapScale(ScaleArgs);
  339.  
  340.                     /* Render the character. */
  341.  
  342.                 BltBitMapRastPort(ScaleDstBitMap,0,SrcY,RPort,WindowLeft + DestX,WindowTop + DestY,SizeX,TextFontHeight,MINTERM_COPY);
  343.             }
  344.  
  345.             DestX += SizeX;
  346.         }
  347.  
  348.         if(!Mode)
  349.             SetFont(ScaleRPort,GFX);
  350.     }
  351.     else
  352.     {
  353.             /* Run down the buffer... */
  354.  
  355.         while(Size--)
  356.         {
  357.             if(*Buffer != ScaleCache)
  358.             {
  359.                 ScaleCache = *Buffer;
  360.  
  361.                     /* Print the character to be scaled into the
  362.                      * invisible drawing area.
  363.                      */
  364.  
  365.                 PlaceText(ScaleRPort,0,0,Buffer++,1);
  366.  
  367.                     /* Scale the font. */
  368.  
  369.                 BitMapScale(ScaleArgs);
  370.             }
  371.             else
  372.                 Buffer++;
  373.  
  374.                 /* Render the character. */
  375.  
  376.             BltBitMapRastPort(ScaleDstBitMap,0,SrcY,RPort,WindowLeft + DestX,WindowTop + DestY,SizeX,TextFontHeight,MINTERM_COPY);
  377.  
  378.             DestX += SizeX;
  379.         }
  380.     }
  381. }
  382.